Java Concurrency / Multithreading Tutorial [Java 并发/多线程教程]

Description:
java 并发文档翻译,文章链接地址http://tutorials.jenkov.com/java-concurrency/index.html



Back in the old days a computer had a single CPU, and was only capable of executing a single program at a time.

回退到旧时光,那时的计算机只有单核并且每次只能够运行一段程序。


Later came multitasking which meant that computers could execute multiple programs(AKA tasks or processes) at the same time.

不久之后多任务计算机登场,多任务计算机指的是那些可以同时运行多段程序(也可以叫做多任务或多进程)的计算机。


It wasn’t really “at the same time” though.

事实上这些任务并非是在同一时间段”并发的”。


The single CPU was shared between the programs.

计算机单核的处理器资源是以共享的方式在多个程序之间被调用。


The operating system would switch between the programs running, executing each of them for a little while before switching.

操作系统也是在多个”同时”执行的程序间来回进行切换,在从一个程序切换到另一个程序的过程中依次执行每段程序中的一小段。


Along with multitasking came new challenges for software developers.

随着多任务计算机时代的来临,软件开发人员也面临着新的跳帧。


Programers can no longer assume to have all the CPU time available, nor all memory or any other computer resources.

程序员再也不能确保随时都能够访问计算机的 CPU ,内存乃至其他计算机的资源了。


A “good citizen” program should release all resources it is no longer using, so other programs can use them.

对于一段”模范”程序来说,应该将对它无用的资源全部释放掉,以确保这些资源可被其他程序所调用。


Later yet came multithread which mean that you could have multiple thread of execution inside the same program.
接下来到来的便是多线程的时代,就是在同一段程序中允许你能同时运行多个线程。


A thread of execution can be thought of as a CPU executing the program.

由一个线程所运行的程序可被等价地看做是由一个 CPU 所执行的程序。


When you have multiple threads executing the same program, it is like having multiple CPUs execute within the same program.

多个线程运行同一段程序,便可以将其比作是多个 CPU 在同一段程序中运行。


Multithreading can be a great way to increase the performance of some types of programs.

多线程这种技术可以极大程度上来提高某种类型程序的效率。


However, multithreading is even more challenging than multitasking.

然而,多线程编程要远比多任务编程面临更多的挑战。


The threads are executing within the same program and are hence reading and writing the same memory simultanously.

由于程序允许多个线程运行在其中,因此这些线程便有可能在同一时间点上的读写相同的内存空间。


This can result in errors not seen in a singlethread program.

上述的这种操作可能引发在单线程运行的程序中遇不到的错误的发生。


Some of these errors may not be seen on single CPU machines, because two threads never really execute “simultanously”.

在引发的错误中有些错误在单核计算机上可能不会遇到,因为运行于单核计算机上的两个线程并不是真正意义上的并发。


Modern computers, though, come with multicore CPUs, and even with multiple CPUs too. This means that separate threads can be executed by separate cores or CPUs simultaneously.

如今的现代计算机都是多核甚至多 CPU 的。 这就意味着可以将线程以 CPU 的核或是 CPU 为单位来实现并发计算。



If a thread reads a memory location while another thread writes to it, what value will the first thread end up reading ?

(设想一下这个场景)如果一个线程在读取一个内存单元空间的时候恰好另一个线程在对这块内存空间执行写入操作的话,第一个线程在执行读操作之后它将获得的数值是什么?


The old value ?

是内存空间中原先的那个数值?


The value written by the second thread ?

还是另一个线程刚刚写入的数值?


Or a value that is mix between the two ?

还是新旧混合的数值?


Or, if two threads are writing to be the same memory location simultanously, what value will be left when they are done ?

又或是,如果两个线程同时向同一块内存空间中执行写入操作的话,当这两个线程执行写操作结束的时候,所写入的内存空间中将会是那个数值?


The value written by the first thread ?

是第一个线程写入的数值?


The value written by the second thread ?

还是第二个线程刚刚写入的数值?


Or a mix of the two values written?

亦或是两个线程写入数值的混合值?


Without proper precautions any of these outcomes are possible.

如果没有适当的提前声明(约定)的话上述的任何一种情况都是有可能发生的。


The behavior would not even be predicable.

执行操作的方式甚至都有可能是不可预知的。


The outcome could change from time to time.

也有可能每次运行都会得到不同的结果。


Therefore it is important as a developer to know how to take the right precautions - meaning learning to control how threads access shared resources like memory, files, databases etc.

因此对于一个开发人员来说认识到如何(根据不同的情景/场合)来做出正确的预防措施 - 也就是说他应该清楚如何控制线程来(安全地)访问一些像是内存,文件,数据库等等诸如此类的共享资源 是十分重要的。


That is one of the topics this java concurrency tutorial addresses.

而(如何在多线程并发的情况下安全地访问共享资源)这便是这篇’java 并发教程’所要向您重点讲述的内容。

Multithreading and Concurrency in Java

Java 中的多线程与并发


Java was one of the first languages to make multithreading easily available to developers.

Java 是第一批实现将多线程功能对开发者而言简单易用的开发语言中的一种。


Java had multithreading capabilities from the very beginging.

Java 这种开发语言在被创造之处就支持多线程编程这种技术。


Therefore, Java developers often face the problems described above.

这也是为何 Java 的开发人员经常会在开发中遇到上描述的众多问题。


That is the reason I am writing this trail on Java concurrency.

而这也正是为何我要编写以 Java 并行技术为主题的这一系列文档。


As notes to myself, and any fellow Java developer whom may benifit from it.

而这一系列文章也将作为我自己的学习笔记,同时希望可以让其他的 Java 开发人员从中受益。


The trail will primarily be concerned with multithreading in Java, but some of the problems occuring in multithreading are similar to problems occuring in multitasking and in distributed systems.


这篇专栏主要关注 Java 开发中的多线程技术,除此之外其实在多任务系统和分布式系统中也会遇到与多线程相似的问题这也是我们讨论的重点。


References to multitasking and distributed systems may therefore occur in this trail too.

考虑到多任务和分布式系统中的相关知识也会在本文中有所提及。


Hence the word “concurrency” rather “multithreading”.

所以在这里我们将这一系列的文章以’并发’而不是’多线程’来进行命名。

2015 年之后和 2015 年之前的 Java 并发技术


A lot has happened in the world of concurrent architecture and design since the first Java concurrency books were written, and even since the Java 5 concurrency utilities were released.

自从第一版关于 Java 并发的书籍问世以及 Java 5 中的并发包被发布之后,并发架构的世界中已经发生了很大的变动。


New, asynchronous “shared-nothing” platforms and APIs like Vert.x and Play/Akka and Qbit have emerged.

新的一种,异步的 “无需共享任何资源” 的平台和API 像是 Vert.x , Play/Akka 和 Qbit 这样的框架相继涌现。


These platforms use a different concurrency model than the standard Java/JEE concurrency model of threading, shared memory and locking.

这种平台相比于标准的 Java/JEE 使用共享内存和所的线程并发模式,采用了完全不同的并发模式。


New non-blocking concurrency algorithms have been published, and new non-blocking tools like the LMax Disrupter have been added to our toolkits.

新式的无阻塞并发算法被推出,同时新型的无阻塞开发工具显示 LMax Disrupter 被键入到我们的开发平台工具中。


New functional programming parallelism has been introduced with the Fork and Join framework in Java7, and the collection streams API in Java8.

基于函数式并发新的编程方法被引入到 Java7 中,同时在 Java8 中对集合流的相关 API 予以支持。


With all these new developments it is about time that I updated this Java Concurrency tutorial.

Java 开发中发生了如此之多的变化,所以这也是我该更新 Java 并发教程手册的时候了。


Therefore, this tutorial is once again work in progress.

这篇 Java 并发教程会一如既往地以不断更新的方式来发布。


New tutorials will be published whenever time is available to write them.

只要我一有时间便会向文章集中添加新的教程文章。

下一篇文章

end